from django.db import models


# 文章表
class Article(models.Model):
    title = models.CharField(max_length=255, verbose_name='文章标题')
    shorter = models.CharField(max_length=32, blank=True, null=True, verbose_name='标题缩写')
    keywords = models.CharField(max_length=255, blank=True, null=True, verbose_name='关键字')
    description = models.CharField(max_length=255, blank=True, null=True, verbose_name='简介')
    pic = models.CharField(max_length=255, blank=True, null=True, verbose_name='封面')
    date_add = models.DateTimeField(auto_now_add=True, verbose_name='添加时间')
    date_update = models.DateTimeField(auto_now=True, verbose_name='修改时间')
    is_active = models.BooleanField(default=False, verbose_name='是否启用 0禁用 1启用')
    is_recommend = models.BooleanField(default=False, verbose_name='是否推荐 0正常 1推荐')
    is_delete = models.BooleanField(default=False, verbose_name='是否删除 1删除 0正常')
    sort = models.ForeignKey(to='sort.Sort', blank=True, null=True, on_delete=models.SET_NULL,
                             db_constraint=False,
                             related_name='article')
    admin = models.ForeignKey(to='admins.Admins', blank=True, null=True, on_delete=models.SET_NULL,
                              db_constraint=False,
                              related_name='article')

    @property
    def content(self):
        return self.article_content.content

    class Meta:
        db_table = 'zeus_article'
        verbose_name = '文章'
        verbose_name_plural = verbose_name

        def __str__(self):
            return self.name


# 文章内容表
class ArticleContent(models.Model):
    content = models.TextField(verbose_name='文章内容', blank=True, null=True)
    article = models.OneToOneField(to='Article', blank=True, null=True, on_delete=models.SET_NULL,
                                   db_constraint=False,
                                   related_name='article_content')

    class Meta:
        db_table = 'zeus_article_content'
        verbose_name = '文章内容'
        verbose_name_plural = verbose_name

        def __str__(self):
            return self.article
